<template>
  <div>
    <a-modal
      :title="formData.id ? '编辑' : '添加'"
      :width="800"
      :confirm-loading="modalLoading"
      :mask-closable="false"
      :destroy-on-close="true"
      v-model="visible"
      @cancel="handleCancel"
      @ok="handleOk"
    >
      <a-form-model ref="formData" :model="formData" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">

        <a-form-model-item label="一级分销比例：" prop="one_rate">
          <a-input-number :min="0" v-model="formData.one_rate" /> %
        </a-form-model-item>
        <a-form-model-item label="二级分销比例：" prop="two_rate">
          <a-input-number :min="0" v-model="formData.two_rate" /> %
        </a-form-model-item>
        <a-form-model-item label="团队极差比例：" prop="team_rate">
          <a-input-number :min="0" v-model="formData.team_rate" /> %
        </a-form-model-item>
        <a-form-model-item label="团队平级比例：" prop="same_rate">
          <a-input-number :min="0" v-model="formData.same_rate" /> %
        </a-form-model-item>
        <a-form-model-item label="平多少级：" prop="same_n">
          <a-input-number :min="0" v-model="formData.same_n" /> 
        </a-form-model-item>

   

      </a-form-model>
    </a-modal>
    <!-- 选择用户 -->
    <selectUser ref="selectUser" type="areaAgent" @select="select"></selectUser>
  </div>
</template>

<script>
import SelectUser from '@/components/SelectUser/newSelectUser'
import { Address } from '@/components'
import { levelSaveApi, levelInfoApi } from '@/api/ad'
import { check } from '@/api/setting/applet'
export default {
  name: 'editForm',
  components: { SelectUser, Address },
  props: {},
  data() {
    return {
      visible: false,
      modalLoading: false,
      formData: {
        id: '',
        one_rate: 0,
        two_rate: 0,
        team_rate: 0,
        same_rate: 0,
        same_n: 0,
      },
      userInfo: {},
      addressInfo: {},
      address_data: {},
    }
  },
  created() {},
  methods: {
    getInfo() {
      var _self = this
      levelInfoApi(_self.formData.id)
        .then((res) => {
          if (res.code == 0) {
            var data = res.data
            _self.formData = data
            // if (data.user) _self.$set(_self.userInfo, 'nickname', data.user.nickname)
            // _self.addressInfo = { province_id: data.province_id, city_id: data.city_id, district_id: data.district_id }
            _self.visible = true
          } else {
            _self.$message.error(res.msg)
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    handleCancel() {
      this.formData = {
        id: '',
        one_rate: 0,
        two_rate: 0,
        team_rate: 0,
        same_rate: 0,
        same_n: 0,
      }
      this.$refs.formData.resetFields()
    },
    handleOk() {
      var _self = this
      _self.$refs.formData.validate((valid) => {
        if (valid) {
          _self.modalLoading = true
          levelSaveApi(_self.formData)
            .then((res) => {
              _self.modalLoading = false
              if (res.code === 0) {
                _self.$message.success(res.msg)
                _self.visible = false
                _self.$emit('confirm')
              } else {
                _self.$message.error(res.msg)
              }
            })
            .catch((err) => {
              _self.modalLoading = false
              console.log(err, 'err')
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    select(e) {
      this.userInfo = e
      this.formData.user_id = e.id
    },
    addressChange(e) {
      this.address_data = e
      if (e.province_id) {
        this.formData.province_id = e.province_id
        this.formData.province = e.province
      }
      if (e.city_id) {
        this.formData.city_id = e.city_id
        this.formData.city = e.city
      }
      if (e.district_id) {
        this.formData.district_id = e.district_id
        this.formData.district = e.district
      }
    },
    levelChange() {
      if (this.address_data.province_id) {
        this.formData.city_id = ''
        this.formData.city = ''
        this.formData.district_id = ''
        this.formData.district = ''
        if (this.formData.area_level >= 2) {
          this.formData.city_id = this.address_data.city_id
          this.formData.city = this.address_data.city
        }
        if (this.formData.area_level >= 3) {
          this.formData.district_id = this.address_data.district_id
          this.formData.district = this.address_data.district
        }
      }
      console.log(this.formData, 222)
    },
  },
}
</script>

<style lang="scss"></style>
